{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取前n个主成分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.empty((100, 2))\n",
    "X[:,0] = np.random.uniform(0., 100., size=100)\n",
    "X[:,1] = 0.75 * X[:,0] + 3. + np.random.normal(0, 10., size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def demean(X):\n",
    "    return X - np.mean(X, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAGulJREFUeJzt3X+MXXWZx/H3w3TQFtcdkIHAFLZ10xQRFosTZO3GIGy2gD+YIG5Eo41L0n9cRdZF664JmjWhBqNi4rrbgMrukgICKUSNaFrIZk3sOrUsPyxdWNS2Q6WjUjTShLY8+8c9F26n59x77vn5Ped+Xkkzc8/cufd75sBzv+f5Puc55u6IiEh7HVf3AEREpFwK9CIiLadALyLScgr0IiItp0AvItJyCvQiIi2nQC8i0nIDA72ZfcPM9pvZYz3bTjKzH5rZk9HXE6PtZmZfNbOnzOwRMzu/zMGLiMhgaWb03wIuXbBtPbDF3VcAW6LHAJcBK6J/64CvFzNMERHJytJcGWtmy4DvuPs50eNdwEXuvs/MTgMecveVZvav0febFj6v3+uffPLJvmzZslw7IiIyarZv3/5rd58c9LxFGV//1G7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Xwtin+P0qlMGvgeujJWRKTlmpa6ERGRISnQi4i0nAK9iEjLKdCLiLScAr2ISMsp0IuItJwCvYhIyynQi4i03P8DBFLS+j4PmAcAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X[:,0], X[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(w, X):\n",
    "    return np.sum((X.dot(w)**2)) / len(X)\n",
    "\n",
    "def df(w, X):\n",
    "    return X.T.dot(X.dot(w)) * 2. / len(X)\n",
    "\n",
    "def direction(w):\n",
    "    return w / np.linalg.norm(w)\n",
    "\n",
    "'''获取第一个主成分'''\n",
    "def first_component(X, initial_w, eta, n_iters = 1e4, epsilon=1e-8):\n",
    "    \n",
    "    w = direction(initial_w) \n",
    "    cur_iter = 0\n",
    "\n",
    "    while cur_iter < n_iters:\n",
    "        gradient = df(w, X)\n",
    "        last_w = w\n",
    "        w = w + eta * gradient\n",
    "        w = direction(w) \n",
    "        if(abs(f(w, X) - f(last_w, X)) < epsilon):\n",
    "            break\n",
    "            \n",
    "        cur_iter += 1\n",
    "\n",
    "    return w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.77160243,  0.63610509])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "initial_w = np.random.random(X.shape[1])\n",
    "eta = 0.01\n",
    "w = first_component(X, initial_w, eta)\n",
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2 = np.empty(X.shape)\n",
    "for i in range(len(X)):\n",
    "    X2[i] = X[i] - X[i].dot(w) * w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8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m9pSkT0i60MyOSrpP0rCkZ8zsi5ImJd2c3AhPx0lbAMiJdk7pAAAqEPABICcI+ACQEwR8AMgJAj4A5AQBHwBygoAPADlBwAeAnPh/N/lw303qeCIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X2[:,0], X2[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2 = X - X.dot(w).reshape(-1, 1) * w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAFehJREFUeJzt3W9sXOWVx/HfwZjKoEoGESiZ4iZCbLawLvauBaksrdpuIdBqy5BdGlBYIbUifVFehEXROgtd/kWLt1lKpKq7WpBQkZJCaDe4aZM2BbJVJYTbOoqpSduI0IKJU5Wg4BctLnWcsy9mrplM7sxc23Pn3jv3+5Eie+5cex4Nk8OT8zzPOebuAgC0v7OSHgAAoDUI+ACQEwR8AMgJAj4A5AQBHwBygoAPADlBwAeAnCDgA0BOEPABICfOTnoAlS688EJfsWJF0sMAgEw5cODA2+6+rNF9qQr4K1as0NjYWNLDAIBMMbM3otxHSgcAcoKADwA5QcAHgJwg4ANAThDwASAnUrVLZ7FGDk5p677DOjY9o+XdXdq0ZpWK/YWkhwUAqZL5gD9ycEqbd01oZnZOkjQ1PaPNuyYkiaAPABUyn9LZuu/wfLAPzMzOaeu+wwmNCADSKfMB/9j0zIKuA0BeZT7gL+/uWtB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Ve78k/bekh1R6Lx6S9IikL7RudKmU28/KAg26+zEzu0jSc2b26/IMFxmRiYDv7p9exI8dlXRpxeMPSzrWnBGlW9T3y8wel/T9mIeTBbn9rCyEux8rf33LzJ5VKRVGwD/T783sEnf/nZldIumtpAcUaOeUzm5Jt5jZB8xspaTLJf0s4TElrvwBDNyk0qJ33v1c0uVmttLMzlFpsX93wmNKFTM7z8w+GHwv6Trx2allt6Tby9/fLqlWFqLlMjHDr8fMbpL0dUnLJO0xs3F3X+Puh8zsGUm/lHRS0pfdfS7JsabEV82sT6WUxeuSvpTscJLn7ifN7E5J+yR1SHrC3Q8lPKy0uVjSs2YmleLGt9z9h8kOKXlm9pSkT0i60MyOSrpP0rCkZ8zsi5ImJd2c3AhPx0lbAMiJdk7pAAAqEPABICcI+ACQEwR8AMgJAj4A5AQBHwBygoAPADlBwAeAnPh/N/lw303qeCIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X2[:,0], X2[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.63611145, -0.77159719])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w2 = first_component(X2, initial_w, eta)\n",
    "w2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.2467824554233182e-06"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w.dot(w2) # 结果接近0，证明两个向量是互相垂直地"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''获取前n个主成分'''\n",
    "def first_n_components(n, X, eta=0.01, n_iters = 1e4, epsilon=1e-8):\n",
    "    X_pca = X.copy()\n",
    "    X_pca = demean(X_pca)\n",
    "    res = []\n",
    "    for i in range(n):\n",
    "        initial_w = np.random.random(X_pca.shape[1])\n",
    "        w = first_component(X_pca, initial_w, eta)\n",
    "        res.append(w)\n",
    "        \n",
    "        X_pca = X_pca - X_pca.dot(w).reshape(-1, 1) * w\n",
    "        \n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([ 0.76699699,  0.6416507 ]), array([-0.64164648,  0.76700052])]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first_n_components(2, X) # 输入时二维数据所以只有两个主成分"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
